package org.scs.carleton.ca.pgdt.core.ui.nature;

import java.lang.reflect.InvocationTargetException;
import java.net.URI;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
import org.eclipse.ui.ide.undo.CreateProjectOperation;
import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
import org.scs.carleton.ca.pgdt.core.ui.Messages;

/**
 * Basic wizard for prolog projects; this simply creates a Prolog Project with a prolog nature.
 * 
 * @author tessierj
 * 
 */
public class BasicPrologProjectWizard extends BasicNewResourceWizard {
	private IProject newProject = null;
	private PrologWizardNewProjectCreationPage wizardPage = null;

	public static final String DEFINED_ID = "org.scs.carleton.ca.pgdt.core.ui.nature.projectWizard"; //$NON-NLS-1$
	private static final String WINDOW_TITLE = "window.title"; //$NON-NLS-1$

	@Override
	public void addPages() {
		super.addPages();
		wizardPage = new PrologWizardNewProjectCreationPage("basicNewProjectPage"); //$NON-NLS-1$
		wizardPage.setTitle(Messages.getString("new.project.title")); //$NON-NLS-1$
		wizardPage.setDescription(Messages.getString("new.project.description")); //$NON-NLS-1$
		this.addPage(wizardPage);
	}

	@Override
	public boolean performFinish() {
		newProject = createProject();

		if (newProject != null) {
			applyProjectNature(newProject);
		} else {
			return false;
		}

		return true;
	}

	private IProject createProject() {
		if (newProject != null)
			return newProject;

		IProject newProjectHandle = wizardPage.getProjectHandle();
		URI location = null;
		if (!wizardPage.useDefaults()) {
			location = wizardPage.getLocationURI();
		}
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		final IProjectDescription description = workspace.newProjectDescription(newProjectHandle.getName());
		description.setLocationURI(location);

		// create the new project operation
		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) throws InvocationTargetException {
				CreateProjectOperation op = new CreateProjectOperation(description, Messages.getString(WINDOW_TITLE));
				try {
					PlatformUI.getWorkbench().getOperationSupport().getOperationHistory().execute(op, monitor, WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
				} catch (ExecutionException e) {
					throw new InvocationTargetException(e);
				}
			}
		};

		// run the new project creation operation
		try {
			getContainer().run(true, true, op);
		} catch (Exception e) {
			// There was an error
			return null;
		}

		newProject = newProjectHandle;

		return newProject;
	}

	private void applyProjectNature(IProject project) {
		try {
			IProjectDescription description = project.getDescription();
			String[] natures = description.getNatureIds();
			String[] newNatures = new String[natures.length + 1];
			System.arraycopy(natures, 0, newNatures, 0, natures.length);
			newNatures[natures.length] = PrologNature.NATURE_ID;
			description.setNatureIds(newNatures);
			project.setDescription(description, null);
		} catch (CoreException e) {
			// Something went wrong
		}
	}

	private class PrologWizardNewProjectCreationPage extends WizardNewProjectCreationPage {
		public PrologWizardNewProjectCreationPage(String pageName) {
			super(pageName);
		}

		public void createControl(Composite parent) {
			super.createControl(parent);
			createWorkingSetGroup((Composite) getControl(), getSelection(), new String[] { "org.eclipse.ui.resourceWorkingSetPage" }); //$NON-NLS-1$
			Dialog.applyDialogFont(getControl());
		}
	}
}
